﻿using AbpDemo.Roles.Dto;
using AbpDemo.Utility;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Entities.Events.Distributed;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace AbpDemo.Roles
{
    [ApiExplorerSettings(GroupName = "v1")]
    public class RoleService : CrudAppExtService<Role, RoleDto, Guid, RoleCreateDto>, IRoleService
    {
        private readonly IRepository<Role, Guid> repository;

        public RoleService(IRepository<Role, Guid> repository) : base(repository)
        {
            this.repository = repository;
        }

        public override async Task<ResultDto<RoleDto>> CreateAsync(RoleCreateDto createInput)
        {
            var entity = await repository.FindAsync(m => m.RoleName == createInput.RoleName);
            if (entity != null)
            {
                return new ResultDto<RoleDto> { Code = ResultState.Failed, Message = "角色重名了" };
            }
            return await base.CreateAsync(createInput);
        }
    }
}
